package com.ruoyi.web.controller.word;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.word.CommonWord;
import com.ruoyi.common.domain.word.CommonWordPhrase;
import com.ruoyi.word.service.ICommonWordPhraseService;
import com.ruoyi.word.service.ICommonWordService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/word/phrase")
public class CommonWordPhraseController extends BaseController {

    @Autowired
    private ICommonWordPhraseService biz;

    @Autowired
    private ICommonWordService wordService;

    @RequestMapping(value = "/findByWid/{wid}", method = RequestMethod.GET)
    public R find(@PathVariable Long wid) {
        try{
            return R.data(biz.findByWid(wid));
        }catch(Exception e){
            e.printStackTrace();
        }
        return R.fail();
    }

    public static final String DETAIL_URL = "http://dict.youdao.com/search?q=${word}&keyfrom=zhiyun";

    // @Scheduled(fixedDelay = 1000*60*60*60*60)
    public R getYoudaoPhrase() {
        try{
            List<CommonWord> wordList = wordService.findByWhereStr(null, "id", "desc");
            wordList.stream().forEach(word -> {
                String url = DETAIL_URL.replace("${word}", word.getName());
                try {
                    Document doc = Jsoup.parse(new URL(url), 3000);
                    if(doc == null) {
                        logger.error("load-doc-err", doc);
                        return;
                    }
                    List<CommonWordPhrase> result = new ArrayList<>();

                    Integer index = 1;
                    if(doc.getElementById("wordGroup2") != null){
                        Elements elements = doc.getElementById("wordGroup2").getElementsByClass("wordGroup");
                        for (Element ele: elements) {
                            String content = ele.getElementsByClass("search-js").first().text();
                            String ph = ele.childNode(2).toString();
                            System.out.println(content);
                            System.out.println(ph);
                            CommonWordPhrase phrase = new CommonWordPhrase(word.getId(), content, ph);
                            phrase.setSort(index++);
                            phrase.setType(2);
                            result.add(phrase);
                        }
                    }

                    if(result.isEmpty() && doc.getElementById("webPhrase") != null) {
                        Elements elements1 = doc.getElementById("webPhrase").getElementsByClass("wordGroup");
                        index = 1;
                        for (Element ele : elements1) {
                            String content = ele.getElementsByClass("search-js").first().text();
                            String ph = "";
                            for (int i = 2; i < ele.childNodeSize(); i++) {
                                Node node = ele.childNode(i);
                                if (node instanceof TextNode) {
                                    ph += ((TextNode) node).text();
                                } else {
                                    ph += ((Element) node).text();
                                }
                            }
                            System.out.println(content.trim());
                            System.out.println(ph.trim());
                            CommonWordPhrase phrase = new CommonWordPhrase(word.getId(), content, ph);
                            phrase.setSort(index++);
                            phrase.setType(1);
                            result.add(phrase);
                        }
                    }
                    biz.saveByWord(word, result);
                    // TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });

            return R.ok();
        }catch (Exception e){
            logger.error("", e);
        }
        return R.fail();
    }

}
